//
//  PlayerCollectionViewCell.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/12.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "PlayerCollectionViewCell.h"
#import "CFMacro.h"
#import "GX_Button.h"
#import "PlayView.h"
#import "UIImageView+WebCache.h"
#import "AlbumRecord_Plays.h"
#import "AlbumRecord.h"
#import "AlbumRecord_Dj_Info.h"
#import "CGX_PlayTool.h"

@interface PlayerCollectionViewCell () <CGX_PlayToolDelegate>

// 播放按钮视图
@property (weak, nonatomic) IBOutlet PlayView *playView;
// 控制器
@property (nonatomic, strong) UIViewController *vc;
// 进度条
@property (weak, nonatomic) IBOutlet UISlider *slider;
// 头像
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *djLabel;
@property (weak, nonatomic) IBOutlet UILabel *playTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeProcessLabel;
@property (nonatomic, strong) CGX_PlayTool *playTool;
@property (nonatomic, strong) NSMutableArray *songList;
@property (nonatomic, assign) NSInteger currentSongIndex;

@end

@implementation PlayerCollectionViewCell

- (void)setRecord_plays:(AlbumRecord_Plays *)record_plays {
    [_songList removeAllObjects];
    _record_plays = record_plays;
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:record_plays.record_dj_info[0].dj_image_url] placeholderImage:[UIImage imageNamed:@"placeholderimage"]];
    _titleLabel.text = record_plays.channel_name;
    _djLabel.text = record_plays.record_dj_info[0].dj_name;
    _sliderLabel.text = record_plays.record[0].record_title;
    _songList = [NSMutableArray array];
    _playTool = [CGX_PlayTool sharedPlayTool];
    _currentSongIndex = 0;
    for (AlbumRecord *record in record_plays.record) {
        [_songList addObject:record.record_file_url];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpMyPlayer];
    [self setUpMySlider];
    [self setUpMyAvatar];
    /**
     * 播放视图
     */
    [self.playView play:^(GX_Button *play) {
        NSLog(@"play");
        _playTool.audioURL = _songList[_currentSongIndex];
        NSLog(@"%@", _songList[_currentSongIndex]);
        NSLog(@"%ld", _songList.count);
        if (_playTool.cgx_player.currentItem) {
            [_playTool playAudio];
        } else {
            [_playTool prepareToPlayAudio];
            [_playTool playAudio];
        }
    } pause:^(GX_Button *pause) {
        NSLog(@"pause");
        [_playTool pauseAudio];
    }];
    [self.playView last:^(GX_Button *last) {
        NSLog(@"last");
        if (_songList.count == 1) {
            _playTool.audioURL = _songList[0];
            NSLog(@"已经是第一首了");
            
        } else {
            if (_currentSongIndex == 0) {
                NSLog(@"已经是第一首了");
            } else {
                _currentSongIndex -= 1;
                _playTool.audioURL = _songList[_currentSongIndex];
                [_playTool prepareToPlayAudio];
            }
        }
    } next:^(GX_Button *next) {
        NSLog(@"next");
        if (_songList.count == 1) {
            _playTool.audioURL = _songList[0];
            NSLog(@"已经是最后一首了");
            
        } else {
            if (_currentSongIndex == _songList.count) {
                NSLog(@"已经是最后一首了");
            } else {
                _currentSongIndex += 1;
                _playTool.audioURL = _songList[_currentSongIndex];
                [_playTool prepareToPlayAudio];
            }
        }
    }];
}
- (void)setUpMyPlayer {
    self.playTool = [CGX_PlayTool sharedPlayTool];
    self.playTool.delegate = self;
    
}
#pragma mark - playTool delegate
- (void)getItemDuration:(NSString *)duration currentSecond:(NSString *)second currentProgress:(CGFloat)currentprogress {
    _timeProcessLabel.text = [NSString stringWithFormat:@"%@/%@", second, duration];
    _slider.value = currentprogress;
}
- (void)getItemLoadedDuration:(CGFloat)loadedDuration {
    NSLog(@"%lf", loadedDuration);
}
- (void)setUpMySlider {
    self.slider.backgroundColor = [UIColor clearColor];
    //设置轨道的图片
    [_slider setMinimumTrackImage:[UIImage imageNamed:@"sliderL"] forState:UIControlStateNormal];
    [_slider setMaximumTrackImage:[UIImage imageNamed:@"sliderR"] forState:UIControlStateNormal];
    //设置滑块的图片
    [_slider setThumbImage:[UIImage imageNamed:@"siderThumb"] forState:UIControlStateNormal];
    //滑动滑块添加事件
    [_slider addTarget:self action:@selector(onThumb:) forControlEvents:UIControlEventValueChanged];
    //滑到最大值的事件
    [_slider addTarget:self action:@selector(endThumb:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setUpMyAvatar {
    self.avatarView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClicked:)];
    [self.avatarView addGestureRecognizer:tap];
}
- (void)avatarClicked:(UITapGestureRecognizer *)tap {
    NSLog(@"点击了主播头像, 跳转主播详情页, 需要数据model");
}
#pragma mark - 滑块移动时调用
- (void)onThumb:(UISlider *)slider {
    [_playTool seekToDragedValue:slider.value];
}
#pragma mark - 滑到最后的时候调用
- (void)endThumb:(UISlider *)slider {
    NSLog(@"%lf", slider.value);
}
#pragma mark - 获取响应者 get VC
- (UIViewController *)vc {
    id responder = self.nextResponder;
    while (![responder isKindOfClass:[UIViewController class]] && responder != nil) {
        responder = [responder nextResponder];
    }
    UIViewController *vc = (UIViewController *)responder;
    return vc;
}
- (IBAction)setTimeButtonClicked:(id)sender {
    //TODO: 定时功能
    NSLog(@"点击定时, 功能");
}
- (IBAction)focusButtonClicked:(id)sender {
    //TODO: 关注功能
    NSLog(@"点击关注, 需要登录");}
- (IBAction)showListButtonClicked:(id)sender {
    //TODO: 播放列表
    NSLog(@"点击列表, 模态一个view");
}
- (IBAction)downloadButtonClicked:(id)sender {
    //TODO: 下载功能
    NSLog(@"点击下载, 功能");
}

@end
